package com.b2j.base;

public class Routine {

	public ProcessData process;
	
	public Routine(ProcessData process) {
		this.process = process;
	}
	
	public Routine() {
		
	}
	
	public String generate(Lexema lexema) {
		
		String line = "";
		
		// If the process type is PROCESS or FUNCTION
		if (process.type != ProcessData.Type.SCOPE) {
			
			boolean firstTime = true;
			if (process.type == ProcessData.Type.PROCESS) {
				line = "new " + MainTools.tools.formatClassName(process.name) + "(this";
			} else {
				line = MainTools.tools.formatClassName(process.name) + ".exec(";
			}
			
			lexema = MainTools.tools.listLexema.get(++MainTools.tools.currentLexema);
	
			if (lexema.tokenType == Token.OPARENT) {
				
				lexema = MainTools.tools.listLexema.get(++MainTools.tools.currentLexema);
				while (lexema.tokenType != Token.CPARENT) {
					
					// At this point, if is the first pass put an ',' to separate the 'this' parameter from the next 
					if (firstTime) {
						if(process.type == ProcessData.Type.PROCESS)
							line = line + ",";
						firstTime=false;
					}
					
					if (lexema.tokenType == Token.ID || lexema.group == TokenGroup.LANGUAGE_COMPONENT) 
						line = line + MainTools.tools.identifier(lexema);
					else
						line = line + lexema.lexema;
			
					lexema = MainTools.tools.listLexema.get(++MainTools.tools.currentLexema);
					
				}
				
			} else {
				ErrorManager.unexpected(lexema);
			}
		
		}
		
		line = line + lexema.lexema;
		
		return line;
		
	}
}
